/*
 * @Description:
 * @Version: V1.0.0
 * @Author: 周艳凯 484894717@qq.com
 * @Date: 2024-11-12 15:44:35
 * @LastEditors: 周艳凯 484894717@qq.com
 * @LastEditTime: 2024-11-12 15:58:28
 * @FilePath: utils.ts
 * Copyright 2024 Marvin, All Rights Reserved.
 * 2024-11-12 15:44:35
 */
const units: [string, number][] = [
  ["Y", 1000 * 60 * 60 * 24 * 365], // years
  ["M", 1000 * 60 * 60 * 24 * 30], // months
  ["D", 1000 * 60 * 60 * 24], // days
  ["H", 1000 * 60 * 60], // hours
  ["m", 1000 * 60], // minutes
  ["s", 1000], // seconds
  ["S", 1], // million seconds
];

export function getDateString(millisecond: number, format: string) {
  let leftMillisecond: number = millisecond;
  return units.reduce((current, [name, unit]) => {
    if (current.indexOf(name) !== -1) {
      const value = Math.floor(leftMillisecond / unit);
      leftMillisecond -= value * unit;
      return current.replace(new RegExp(`${name}+`, "g"), (match: string) => {
        const len = match.length;
        return String(value).padStart(len, "0");
      });
    }
    return current;
  }, format);
}
